package com.example.obs.config;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Author Kevin
 * @Description 自定义角色转换器，String->Integer
 * @Date 2020/9/18 15:51
 **/
public class UserRoleConverter implements Converter<Integer> {

    private static final String ADMIN = "管理员";
    private static final String SP_ADMIN = "超级管理员";

    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value = cellData.getStringValue();
        if (ADMIN.equals(value)) {
            return 1;
        } else if (SP_ADMIN.equals(value)) {
            return 2;
        }
        return 0;
    }

    /**
     * 这里是写的时候会调用 不用管
     *
     * @param s                    NotNull
     * @param excelContentProperty Nullable
     * @param globalConfiguration  NotNull
     * @return
     */
    @Override
    public CellData convertToExcelData(Integer s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData(s);
    }
}
